using ASP;
using HoWave.Alipay;
using HoWave.Common;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
public class Member_MBF_PayVipCards : Member, IRequiresSessionState
{
	protected HiddenField h_extra_common_param;
	protected DropDownList PayType;
	protected TextBox TxtMoney;
	protected Literal ltr_notice;
	protected Button Button1;
	protected Button Button2;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			int iD = Fetch.GetID("count");
			decimal num = Text.ToDecimal(Fetch.Get("FaceValue"));
			int iD2 = Fetch.GetID("CardStoreID");
			if (iD < 1 || num < 0m)
			{
				this.WriteErrMsg("", Language.GetMemberMsg("Voucher/DataIllegal", new string[0]), "");
			}
			HoWave.Data.Model.UserCardStore model = Blls.bllUserCardStore.GetModel(iD2);
			if (model == null || model.FaceValue != num)
			{
				this.WriteErrMsg("", Language.GetMemberMsg("Voucher/DataIllegal", new string[0]), "");
			}
			this.ltr_notice.Text = string.Format("{0}张面值为{1}的{2}", iD, model.FaceValue, Blls.bllUserCardCategory.GetNameByCategory(model.CardCategory));
			this.h_extra_common_param.Value = string.Format("{0}-{1}_{2}", iD2, iD, num);
			this.TxtMoney.Text = decimal.Round(iD * num, 2).ToString();
			this.TxtMoney.ReadOnly = true;
			this.BindPayType();
		}
	}
	protected void BindPayType()
	{
		string key = this.config["AlipayKey"].ToString().Trim();
		OnlinePaySeting onlinePaySeting = new OnlinePaySeting(this.conn);
		this.PayType.DataSource = onlinePaySeting.GetActivePaySeting(key);
		this.PayType.DataBind();
	}
	protected void Button1_Click(object sender, EventArgs e)
	{
		string text = this.config["AlipayPartnerID"].ToString().Trim();
		if (this.PayType.SelectedIndex == -1 || this.PayType.SelectedValue == "")
		{
			this.WriteErrMsg("", "请选择通道！", "");
		}
		int num = Convert.ToInt32(this.PayType.SelectedValue);
		decimal num2 = 0m;
		num2 = Text.ToDecimal(this.TxtMoney.Text);
		if (num2 <= 0m)
		{
			this.WriteErrMsg("", Language.GetMemberMsg("Voucher/MoneyErr", new string[0]), "");
		}
		num2 = decimal.Round(num2, 2);
		PayOnline payOnline = new PayOnline(this.conn);
		string text2 = DateTime.Now.ToString("yyyyMMddhhmmss") + Text.MakePassword(2);
		if (num < 0)
		{
			this.WriteErrMsg("", Language.Get("Common/NotData"), "");
		}
		if (num >= 0)
		{
			if (!payOnline.Add(base.user.UserID, base.user.UserName, 0, text2, num, num2, num2, "", ""))
			{
				this.WriteErrMsg("", Language.Get("Common/Fail", ""), "");
			}
		}
		if (num == 0)
		{
			if (base.user.Balance < num2)
			{
				this.WriteErrMsg("", Language.GetMemberMsg("Order/PayOrder/NotBalance", new string[0]), "");
			}
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@Remark", DbType.String, "商城币支付vip卡");
			this.conn.BeginTransaction();
			try
			{
				decimal d = (decimal)this.conn.GetScalar("Select Balance From [HL_User] Where UserID=" + base.user.UserID, new IDbDataParameter[0]);
				string userBalanceKey = Text.GetUserBalanceKey(base.user.UserID, d - num2);
				this.conn.ExecuteTransaction(string.Concat(new object[]
				{
					"Update [HL_User] Set Balance=Balance-",
					num2,
					",EncryptMoney='",
					userBalanceKey,
					"' Where UserID=",
					base.user.UserID
				}));
				this.conn.ExecuteTransaction(string.Concat(new object[]
				{
					"Insert Into HL_AccountDetails(UserID,cMoney,cType,uBalance,CreateTime,Remark) values(",
					base.user.UserID,
					",",
					num2,
					",0,",
					d - num2,
					",getdate(),@Remark)"
				}), new IDbDataParameter[]
				{
					dbDataParameter
				});
			}
			catch (Exception ex)
			{
				this.conn.Rollback();
				ex.WriteErrorLog(new string[0]);
				this.WriteErrMsg("", Language.Get("Common/Fail", ""), "");
			}
			this.conn.Commit();
			base.user.Balance = base.user.Balance - num2;
			payOnline.SetPayOK(text2);
			string[] array = this.h_extra_common_param.Value.Split(new char[]
			{
				'-'
			});
			string[] array2 = array[1].Split(new char[]
			{
				'_'
			});
			int num3 = Convert.ToInt32(array2[0]);
			decimal num4 = Convert.ToDecimal(array2[1]);
			if (num2 != num4 * num3)
			{
				this.WriteErrMsg("", Language.GetMemberMsg("Voucher/DataIllegal", new string[0]), "");
			}
			int cardStoreID = Convert.ToInt32(array[0]);
			int num5 = (int)this.conn.GetScalar("Select UserID From HL_PayOnline WHERE PayNum= '" + text2 + "'", new IDbDataParameter[0]);
			if (!this.AddUserVipBuyedCard(num3, cardStoreID, num4))
			{
				this.WriteErrMsg("", Language.GetMemberMsg("Voucher/AddVipError", new string[0]), "");
			}
			this.WriteSuccessMsg("", "VIP卡购买成功！", "MBF_BuyVipCards.aspx");
		}
		else
		{
			if (num > 0)
			{
				OnlinePaySeting onlinePaySeting = new OnlinePaySeting(this.conn);
				DataTable paySetingByID = onlinePaySeting.GetPaySetingByID(num);
				if (paySetingByID.Rows.Count <= 0)
				{
					this.WriteErrMsg("", Language.Get("Common/NotData"), "");
				}
				string text3 = paySetingByID.Rows[0]["SysStr"].ToString();
				string value = DateTime.Now.ToString("yyyyMMddhhmmss") + Text.MakePassword(2);
				string value2 = "购买VIP卡";
				string text4 = this.ltr_notice.Text;
				string[] array3 = this.h_extra_common_param.Value.Split(new char[]
				{
					'-'
				})[1].Split(new char[]
				{
					'_'
				});
				decimal d2 = Convert.ToDecimal(array3[0]) * Convert.ToDecimal(array3[1]);
				decimal d3 = Convert.ToDecimal(this.TxtMoney.Text.Trim());
				if (d2 != d3)
				{
					this.WriteErrMsg("", Language.GetMemberMsg("Voucher/DataIllegal", new string[0]), "");
				}
				string value3 = this.TxtMoney.Text.Trim();
				string value4 = "";
				string value5 = "";
				string value6 = "";
				string value7 = "";
				string value8 = "";
				string value9 = this.h_extra_common_param.Value;
				string value10 = "";
				string value11 = "";
				string value12 = "";
				string value13 = text;
				string value14 = this.config["AlipayEmail"].ToString().Trim();
				string value15 = this.config["AlipayKey"].ToString().Trim();
				string value16 = this.config["SiteUrl"].ToString().Trim() + "PayReturn/AliPay/Recharge/payvip_return_url.aspx";
				string value17 = this.config["SiteUrl"].ToString().Trim() + "PayReturn/AliPay/Recharge/payvip_notify_url.aspx";
				SortedDictionary<string, string> sortedDictionary = new SortedDictionary<string, string>();
				sortedDictionary.Add("partner", value13);
				sortedDictionary.Add("key", value15);
				sortedDictionary.Add("seller_email", value14);
				sortedDictionary.Add("return_url", value16);
				sortedDictionary.Add("notify_url", value17);
				sortedDictionary.Add("payment_type", "1");
				sortedDictionary.Add("show_url", value8);
				sortedDictionary.Add("out_trade_no", value);
				sortedDictionary.Add("subject", value2);
				sortedDictionary.Add("body", text4);
				sortedDictionary.Add("total_fee", value3);
				sortedDictionary.Add("paymethod", value4);
				sortedDictionary.Add("defaultbank", value5);
				sortedDictionary.Add("anti_phishing_key", value6);
				sortedDictionary.Add("exter_invoke_ip", value7);
				sortedDictionary.Add("extra_common_param", value9);
				sortedDictionary.Add("buyer_email", value10);
				sortedDictionary.Add("royalty_type", value11);
				sortedDictionary.Add("royalty_parameters", value12);
				Service service = new Service();
				string s = service.Create_direct_pay_by_user(sortedDictionary);
				base.Response.Write(s);
			}
		}
	}
	private bool AddUserVipBuyedCard(int count, int CardStoreID, decimal FaceValue)
	{
		bool flag = false;
		bool result;
		if (count < 1 || CardStoreID < 1 || FaceValue < 0m)
		{
			result = false;
		}
		else
		{
			HoWave.Data.Model.UserCardStore model = Blls.bllUserCardStore.GetModel(CardStoreID);
			if (model == null)
			{
				flag = false;
			}
			else
			{
				HoWave.Data.Model.UserVipCard userVipCard = new HoWave.Data.Model.UserVipCard();
				UserCardRecordModel userCardRecordModel = new UserCardRecordModel();
				userCardRecordModel.Amount = model.FaceValue;
				userCardRecordModel.Remark = "购买VIP卡";
				userCardRecordModel.CardType = "1";
				userCardRecordModel.UserID = new int?(base.user.UserID);
				userVipCard.CardCategory = model.CardCategory;
				userVipCard.FaceValue = model.FaceValue;
				userVipCard.Balance = model.FaceValue;
				userVipCard.PurchaseDate = new DateTime?(DateTime.Now);
				userVipCard.PurchaseUserID = new int?(base.user.UserID);
				userVipCard.CouponAmount = model.FaceValue * 0.01m * Convert.ToDecimal(model.DiscountRate);
				for (int i = 0; i < count; i++)
				{
					long num = Blls.bllUserVipCard.Add(userVipCard);
					this.conn.Execute(string.Concat(new object[]
					{
						"UPDATE [HL_User] SET [Balance] = [Balance]+",
						userVipCard.CouponAmount,
						" WHERE [UserID] = ",
						base.user.UserID
					}));
					userCardRecordModel.CardNumber = Conn.GetSingle("select Top 1 CardNumber from MBF_UserVipCard where id=" + num, new IDbDataParameter[0]).IsNull(new object[0]);
					if (userCardRecordModel.CardNumber != "")
					{
						Blls.bllUserCardRecord.Add(userCardRecordModel);
					}
					flag = true;
				}
			}
			result = flag;
		}
		return result;
	}
}
